package com.fenghuajueli.idiomppp.ui.idiompin;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.adapter.IdiomSelectPassAdapter;
import com.fenghuajueli.idiomppp.base.JumpActivityUtils;
import com.fenghuajueli.idiomppp.databinding.ActivityIdiomSelectPassBinding;
import com.fenghuajueli.idiomppp.entity.GuessIdiomEntity;
import com.fenghuajueli.idiomppp.entity.IdiomPinParentEntity;
import com.fenghuajueli.idiomppp.entity.IdiomXiaoDbEntity;
import com.fenghuajueli.idiomppp.utils.MmkvKey;
import com.fenghuajueli.idiomppp.utils.RuntimeDataUtils;
import com.fenghuajueli.libbasecoreui.baseswitch.PublicFunction;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.libbasecoreui.mvp.LifecycleBaseActivity;
import com.fenghuajueli.libbasecoreui.utils.ImageLoadManager;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;
import com.fenghuajueli.libbasecoreui.utils.MyStatusBarUtils;

import java.util.List;


/**
 * 成语接龙选择关卡界面
 */
public class IdiomJieLongSelectPassActivity extends LifecycleBaseActivity<IdiomSelectPassPresenter> implements IdiomSelectPassView {

    RelativeLayout containerView;
    GridView gridView;
    ImageView ivBtnUp;
    ImageView ivBtnNext;
    ImageView tipImage;
    TextView tvTitle;
    IdiomSelectPassAdapter idiomSelectPassAdapter;
    private int passLevel = 1800;
    private Boolean isFirstLoad = false;

    private ActivityIdiomSelectPassBinding binding;

    private void initview() {
        containerView = binding.containerView;
        gridView = binding.gridView;
        ivBtnUp = binding.ivBtnUp;
        ivBtnNext = binding.ivBtnNext;
        tipImage = binding.tipImage;
        tvTitle = binding.tvTitle;
        OnClick(binding.getRoot());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityIdiomSelectPassBinding.inflate(getLayoutInflater());
        MyStatusBarUtils.setStatusBar(this, "#FFB856");
        setContentView(binding.getRoot());
        initview();
        ImageLoadManager.loadForViewGroup(containerView, R.mipmap.aa_szxl_bj);
        tvTitle.setText("成语接龙");
        findViewById(R.id.ivBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        passLevel = MmkvUtils.mmkv.decodeInt(MmkvKey.IdiomJieLongPass.name(), 1800);
        presenter.initJieLongData();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IdiomSelectPassPresenter initPresenter() {
        return new IdiomSelectPassPresenter(this);
    }

    @Override
    public void loadSuccess(List<IdiomPinParentEntity> idiomPinParentEntityList) {
        if (idiomSelectPassAdapter == null) {
            idiomSelectPassAdapter = new IdiomSelectPassAdapter(IdiomJieLongSelectPassActivity.this, idiomPinParentEntityList);
            idiomSelectPassAdapter.setPassLevel(passLevel);
            idiomSelectPassAdapter.setJieLongMode(true);
            gridView.setAdapter(idiomSelectPassAdapter);
            idiomSelectPassAdapter.setOnBaseClick(new OnListClickListener<IdiomPinParentEntity>() {
                @Override
                public void itemClick(int position, IdiomPinParentEntity data) {
                    RuntimeDataUtils.idiomPinParentEntity = data;
                    JumpActivityUtils.goNormalActivity(IdiomJieLongSelectPassActivity.this, IdiomPinPlayActivity.class, true);
                }
            });
        } else {
            passLevel = MmkvUtils.mmkv.decodeInt(MmkvKey.IdiomJieLongPass.name(), 1800);
            idiomSelectPassAdapter.setPassLevel(passLevel);
            idiomSelectPassAdapter.refreshData(idiomPinParentEntityList);
        }
    }

    @Override
    public void changeBtnUpView(int viewStatus) {
        ivBtnUp.setVisibility(viewStatus);
    }

    @Override
    public void changeBtnNextView(int viewStatus) {
        ivBtnNext.setVisibility(viewStatus);
    }

    @Override
    public void loadXiaoDataSuccess(List<IdiomXiaoDbEntity> idiomPinParentEntityList) {

    }

    @Override
    public void loadGuessDataSuccess(List<GuessIdiomEntity> idiomPinParentEntityList) {

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!isFirstLoad) {
            isFirstLoad = true;
        } else {
            passLevel = MmkvUtils.mmkv.decodeInt(MmkvKey.IdiomJieLongPass.name(), 1800);
            idiomSelectPassAdapter.setPassLevel(passLevel);
            idiomSelectPassAdapter.notifyDataSetChanged();
        }
    }

    public void OnClick(View view) {

        int id = view.getId();
        if (id == R.id.ivBtnUp) {
            presenter.getJieLongUpPageData();
        } else if (id == R.id.ivBtnNext) {
            presenter.getJieLongNextPageData();
        }
    }

}
